package com.cs.service;

import com.cs.mapper.BehospitalMapper;
import com.cs.pojo.Behospital;
import com.cs.pojo.BehospitalExample;
import com.cs.pojo.People;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BeHospitalService {
    
    @Resource
    private BehospitalMapper behospitalMapper;
    @Resource
    private PeopleService peopleService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW , readOnly = true)
    public Behospital queryById(int id){
        return behospitalMapper.selectByPrimaryKey(id);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW , readOnly = true)
    public PageInfo<Behospital> queryByPage(Integer pageNum, Integer pageSize,int peopleId){
    
        BehospitalExample example = new BehospitalExample();
        BehospitalExample.Criteria criteria = example.createCriteria();
        
        if (peopleId > 0) {
            criteria.andBehIdEqualTo(peopleId);
        }
    
        PageHelper.startPage(pageNum , pageSize);
        List<Behospital> bellhops = behospitalMapper.selectByExample(example);
        for (Behospital b : bellhops) {
            People people = peopleService.queryById(b.getBehId());
            b.setBehName(people.getBehName());
        }
        
        return new PageInfo<>(bellhops);
    }
    
}
